#include "board.h"
#include "rtthread.h"

static void Receive_Thread_Rntry(void* parameter);
static void Send_Thread_Entry(void* parameter);

static rt_thread_t receive_thread = RT_NULL;
static rt_thread_t send_thread = RT_NULL;

static rt_event_t test_event = RT_NULL;

#define KEY1_EVENT (0x01 << 0)
#define KEY2_EVENT (0x01 << 1)

int main(void)
{	
		ILI9341_GramScan ( 6 );//设置显示模式
		LED_Color(LED_OFF);
		test_event = rt_event_create("test",RT_IPC_FLAG_FIFO);
	
    receive_thread = 
			rt_thread_create("receive_thread",Receive_Thread_Rntry,RT_NULL,512,3,20);
	  rt_thread_startup(receive_thread);
	
		send_thread = 
				rt_thread_create("send_thread",Send_Thread_Entry,RT_NULL,512,3,20);
		rt_thread_startup(send_thread);	
}

static void Receive_Thread_Rntry(void* parameter)
{
	rt_uint32_t recved;
	int flag;
	while(1)
	{
		rt_event_recv(test_event,
									KEY1_EVENT | KEY2_EVENT,
									RT_EVENT_FLAG_AND | RT_EVENT_FLAG_CLEAR,
									RT_WAITING_FOREVER,
									&recved);
		
		if (recved == (KEY1_EVENT|KEY2_EVENT))
		{
			flag = !flag;
	
		}
		LED_Color(flag);
		rt_thread_delay(1000);
	}
}

static void Send_Thread_Entry(void* parameter)
{
	rt_err_t uwRet = RT_EOK;
	while(1)
	{
		if(GET_KEY1)
		{
			rt_event_send(test_event,KEY1_EVENT);
		}
		if(GET_KEY2)
		{
			rt_event_send(test_event,KEY2_EVENT);
		}
		rt_thread_delay(20);
	}
}
